import { Prefab, _decorator, instantiate } from 'cc';
import { AbNames, EventMsgKes } from '../../Config/ResConfig';
import { IThemeMatchPPBroad } from './GameHallProto';
import { UIMgr, UIType } from '../../../../Framework/Scripts/Managers/UIMgr';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { ViewMgr } from '../../../../Framework/Scripts/Managers/ViewMgr';
import { Respones } from '../../Config/GameCfg';
import { SceneMgr } from '../../../../Framework/Scripts/Managers/SceneMgr';
import { ResManager } from '../../../../Framework/Scripts/Managers/ResManager';
const { ccclass, property } = _decorator;

//挂载到 root常驻节点上的，因为一些消息是广播，放在常驻节点上
@ccclass('GameHallCom')
export class GameHallCom extends UIComponent {
    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        console.log('============GameHallCom==============')
        this.InitData();
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();
    }

    private InitData(){
   
    }

    private registerEvent(){
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgIThemeMatchPPBroad,this._onThemeMatchPPBroad,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgThemeMatchStarBroad,this._onThemeMatchStartGameBroad,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgThemeMatchJieSanBroad,this._onThemeMatchJieSanBroad,this);
    }

    private unRegisterEvent(){
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgIThemeMatchPPBroad,this._onThemeMatchPPBroad,this);
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgThemeMatchStarBroad,this._onThemeMatchStartGameBroad,this);
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgThemeMatchJieSanBroad,this._onThemeMatchJieSanBroad,this);
        
    }

    //主题赛 --匹配成功广播
    private _onThemeMatchPPBroad(data:IThemeMatchPPBroad){
        console.log('=======_onThemeMatchPPBroad=========',data);
        if(this.hasProInObj(data,'gameNet')){
            // 记录下游戏服务ip和port
            globalThis.gameMgr.gameNet = data.gameNet;
                                
            let parent = UIMgr.inst.getParent(UIType.UnFull);
            let pre = ResManager.inst.getAsset(AbNames.Common,'Prefabs/Loading',Prefab) as Prefab;
            let node = instantiate(pre);
            parent.addChild(node);

            //加载大厅资源
            SceneMgr.inst.LoadGameRes(()=>{
                console.log('=========大厅资源加载完毕==========')
                //加载完毕链接网络
                let dwType = globalThis.gameMgr.zts_type ;
                ViewMgr.inst.ConnectToGame(dwType);
            })
        }
    }

    //主题赛----开始游戏广播个
    private _onThemeMatchStartGameBroad(data: any){
        console.log('=======_onThemeMatchStartGameBroad=========');
        // if(this.hasProInObj(data,'code')){
        //     if(data.code === Respones.OK){
        //         if(this.hasProInObj(data,'gameNet')){
        //             // 记录下游戏服务ip和port
        //             globalThis.gameMgr.gameNet = data.gameNet;
                    
        //             let parent = UIMgr.inst.getParent(UIType.UnFull);
        //             let pre = ResManager.inst.getAsset(AbNames.Common,'Prefabs/Loading',Prefab) as Prefab;
        //             let node = instantiate(pre);
        //             parent.addChild(node);

        //             //加载大厅资源
        //             SceneMgr.inst.LoadGameRes(()=>{
        //                 console.log('=========大厅资源加载完毕==========')
        //                 //加载完毕链接网络
        //                 let dwType = globalThis.gameMgr.zts_type ;
        //                 ViewMgr.inst.ConnectToGame(dwType);
        //             })
        //         }else{
        //             console.log('====_onThemeMatchStartGameBroad not has gameNet=====');
        //         }
        //     }else{
        //         console.log('====_onThemeMatchStartGameBroad error =====',data.code);
        //     }
        // }
    }

    // 玩家取消匹配，广播结果
    _onThemeMatchJieSanBroad(data:any){
        console.log('==========_onThemeMatchJieSanBroad==========',data);
        UIMgr.inst.ShowUIView(AbNames.GameHall,'Prefabs/GameStart1/GameStart1', UIType.Full)
    }
}


